package 剑指offer;

import java.util.Arrays;

public class Offer21_JZ13 {
    public static int[] reOrderArray (int[] array) {
        if (array.length <= 1) {
            return array;
        }
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            while (array[left]%2==1 && left<right) {
                left++;
            }
            while (right<array.length && array[right]%2==0 && left<right) {
                right--;
            }
            if (left < right) {
                swap(array, left, right);
            }
        }
        return array;
    }
    public static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(reOrderArray(new int[]{1,2,3,4})));
        System.out.println(Arrays.toString(reOrderArray(new int[]{2,1,3,4})));
    }
}
